/**
 * 
 */
package cn.edu.hstc.ui.fragment;

import java.io.InputStream;

import android.graphics.BitmapFactory;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import cn.edu.hstc.R;
import cn.edu.hstc.app.App;
import cn.edu.hstc.bean.User;
import cn.edu.hstc.bean.utils.UserUtils;
import cn.edu.hstc.common.HttpUtils;
import cn.edu.hstc.common.L;
import cn.edu.hstc.ui.JwglMain;
import cn.edu.hstc.ui.LoginJwgl;

/**
 * @Description 用户信息展示
 * @author act262
 * @version 1.0
 * @since 2014-4-30 下午5:29:16
 * 
 */
public class UserInfo extends BaseFragment {

	/** 根视图 */
	private View mRootView;

	/** httpClient */
	private AndroidHttpClient mHttpClient;

	/** 获取用户 */
	private GetUserTask getUserTask = null;
	/** 获取头像 */
	private GetHeadTask getHeadTask = null;

	/** 单例 */
	private static UserInfo sFragment = null;

	private User mUser;

	private Handler mHandler;

	private final String TAG = UserInfo.class.getSimpleName();

	/**
	 * 使用单例模式获取该实例对象
	 * 
	 * @param bundle
	 *            传递的数据
	 * @return
	 */
	public static UserInfo getInstance(Bundle bundle) {
		if (null == sFragment) {
			L.d("getInstance");
			sFragment = new UserInfo();
		}
		sFragment.setArguments(bundle);
		return sFragment;
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		L.d(TAG, "onCreate");

		init();

		mHandler = new Handler() {
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 0x00:
					showToast(getActivity(), "头像加载失败");
					break;
				case 0x01:
					((ImageView) mRootView.findViewById(R.id.iv_user_info_head)).setImageBitmap(BitmapFactory.decodeStream((InputStream) msg.obj));
					break;

				default:
					break;
				}
			}
		};
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		L.d(TAG, "onCreateView");
		mRootView = inflater.inflate(R.layout.user_info, null);
		getHeadTask = new GetHeadTask();
		getHeadTask.execute(mUser.getStudentId(), mUser.getName());
		return mRootView;
	}

	private void init() {
		mHttpClient = JwglMain.sHttpClient;
		// 获取用户信息
		getUserTask = new GetUserTask();
		mUser = App.getUser();
		getUserTask.execute(mUser.getStudentId(), mUser.getName());
	}

	public void onDestroy() {
		super.onDestroy();
		if (null != getUserTask && AsyncTask.Status.RUNNING == getUserTask.getStatus()) {
			getUserTask.cancel(true);
		}
		if (null != getHeadTask && AsyncTask.Status.RUNNING == getHeadTask.getStatus()) {
			getHeadTask.cancel(true);
		}
	}

	/**
	 * @Description 获取用户信息任务
	 * 
	 */
	private class GetUserTask extends AsyncTask<String, Integer, User> {

		protected User doInBackground(String... params) {
			L.d(TAG, "GetUserTask doInBackground");
			if (isCancelled()) {
				return null;
			}
			// 参数:学号,姓名
			return UserUtils.getUser(mHttpClient, params);
		}

		protected void onPreExecute() {
			super.onPreExecute();
			L.d(TAG, "GetUserTask start");
			// 获取信息中...
			showLoading(true);
		}

		protected void onPostExecute(User result) {
			L.d(TAG, "GetUserTask end");
			if (null != result) {
				((TextView) mRootView.findViewById(R.id.tv_user_info_show)).setText(result.toString());
				mUser = result;
			} else {
				showToast(getActivity(), "获取信息失败");
			}
			showLoading(false);
		}

	}

	/**
	 * @Description 获取用户头像信息的异步任务
	 * 
	 */
	private final class GetHeadTask extends AsyncTask<String, Integer, InputStream> {

		protected InputStream doInBackground(String... params) {
			L.d(TAG, "GetHeadTask doInBackground");
			if (isCancelled()) {
				return null;
			}
			return HttpUtils.getHeadStream(mHttpClient, params);
		}

		protected void onPreExecute() {
			L.d(TAG, "GetHeadTask start");
			super.onPreExecute();
		}

		protected void onPostExecute(InputStream result) {
			L.d(TAG, "GetHeadTask end ");
			if (null != result) {
				Message msg = new Message();
				msg.obj = result;
				msg.what = 0x01;
				mHandler.sendMessage(msg);
			} else {
				mHandler.sendEmptyMessage(0x00);
			}
		}

	}

}
